package com.yunxi.service.tpf.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.bizToHub.pojo.TPFCreditInvestigationVO;
import com.yunxi.bizToHub.pojo.TPFPatchBoltVO;
import com.yunxi.bizToHub.service.TpfDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2019/9/16.
 */
class TpfDubboServiceImpl implements TpfDubboService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(TpfDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${YCYJ.JJ.URL}")
    private String YCYJ_JJ_URL;
    @Value("${YCYJ.BJ.URL}")
    private String YCYJ_BJ_URL;
    @Override
    public ApiResult sendCreditInvestigationPms2YCYJ(TPFCreditInvestigationVO var1) {
        String creditInvestigation = JSONObject.toJSONString(var1);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(creditInvestigation);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统进件信息给有车有家系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TPF_CREDITLNVESTIGATION);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(creditInvestigation, Constant.PMS_TPF_CREDITLNVESTIGATION, Constant.PMS, YCYJ_JJ_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(YCYJ_JJ_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("0")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    @Override
    public ApiResult sendPatchBoltVOPms2YCYJ(TPFPatchBoltVO var1) {
        String creditInvestigation = JSONObject.toJSONString(var1);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(creditInvestigation);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统传递补件信息给有车有家系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TPF_PATCHBOLT);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(creditInvestigation, Constant.PMS_TPF_PATCHBOLT, Constant.PMS, YCYJ_BJ_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(YCYJ_BJ_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (!String.valueOf(jsonResponse.get("code")).equals("0")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }
}
